package com.weikuang.fareast.activityNew;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.trello.rxlifecycle2.android.ActivityEvent;
import com.weikuang.fareast.R;
import com.weikuang.fareast.activity.BaseActivity;
import com.weikuang.fareast.adapter.HotelAdapter;
import com.weikuang.fareast.adapter.NumberAdapter;
import com.weikuang.fareast.bean.BranchCodeList;
import com.weikuang.fareast.bean.CustomerBean;
import com.weikuang.fareast.bean.HotelBean;
import com.weikuang.fareast.http.MainHttp;
import com.weikuang.fareast.network.KillerApi;
import com.weikuang.fareast.network.MainApi;
import com.weikuang.fareast.network.MyObserve;
import com.weikuang.fareast.utils.UserHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.dlc.commonlibrary.okgo.rx.OkObserver;
import cn.dlc.commonlibrary.ui.widget.TitleBar;
import cn.dlc.commonlibrary.utils.ToastUtil;
import cn.dlc.commonlibrary.utils.rv_tool.RecyclerViewUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 获取分行编号
 */

public class NumberActivity extends BaseActivity {
    @BindView(R.id.mTitleBar)
    TitleBar mTitleBar;
    @BindView(R.id.ed_number)
    EditText edNumber;
    @BindView(R.id.btn_goods_sure)
    Button btnGoodsSure;
    @BindView(R.id.recycler)
    RecyclerView mRecyclerView;


    private NumberAdapter mNumberAdapter;
    private BranchCodeList.DataBean branchCode ;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_number;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);


        edNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


            }

            @Override
            public void afterTextChanged(Editable editable) {
                getCustomerList();
            }
        });
        InitRecyclerView();
       // getUser();
        getCustomerList();
        btnGoodsSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent();
                i.putExtra("data",branchCode);
                setResult(4, i);
                finish();

            }
        });
    }

    private void InitRecyclerView() {
        mNumberAdapter = new NumberAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        RecyclerViewUtil.setDefaultDivider(mRecyclerView, linearLayoutManager);
        mRecyclerView.setAdapter(mNumberAdapter);

        mNumberAdapter.setOnItemClickListener(new NumberAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, BranchCodeList.DataBean data, int position) {
                edNumber.setText(data.getBranchCode());
                branchCode = data;
            }
        });

    }



    private void getCustomerList() {
        String keyword = edNumber.getText().toString();
        if (TextUtils.isEmpty(keyword)){
            keyword = "";
        }
        MainApi.getInstance().getApi( KillerApi.class)
                .getBranchCodeList(UserHelper.get().getUserid(),keyword)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new MyObserve<BranchCodeList>() {
                    @Override
                    public void onNetError(Throwable e) {
                        showOneToast(getString(R.string.order_34));
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BranchCodeList branchCodeList) {
                        if (branchCodeList.getData()!= null && branchCodeList.getData().size() == 0){
                            ToastUtil.show(getActivity(),getString(R.string.order_57));
                        }
                        mNumberAdapter.setNewData(branchCodeList.getData());
                    }
                });
    }
}
